/*
 * @Date        : 2020-12-02 10:29:25
 * @LastEditors : anlzou
 * @Github      : https://github.com/anlzou
 * @LastEditTime: 2021-01-10 12:39:27
 * @FilePath    : \code\elm-manage-business\src\store\index.js
 * @Describe    :
 */
import Vue from 'vue'
import Vuex from 'vuex'
import { getAdminInfo } from '@/api/getData'

Vue.use(Vuex)

// const state = {
// 	state:sessionStorage.getItem('state') ? JSON.parse(sessionStorage.getItem('state')): {
// 	adminInfo: {
// 		avatar: 'default.jpg'
// 	},
// 	user: { count: 0 },
// 	foods: { count: 0 },
// 	shop_name: null,
// 	shop_id: null
// }

const mutations = {
	saveAdminInfo(state, adminInfo) {
		state.adminInfo = adminInfo;
	}
}

const actions = {
	async getAdminData({ commit }) {
		try {
			const res = await getAdminInfo()
			if (res.status == 1) {
				commit('saveAdminInfo', res.data);
			} else {
				throw new Error(res.type)
			}
		} catch (err) {
			// console.log(err.message)
		}
	}
}

export default new Vuex.Store({
	actions,
	mutations,
	state: sessionStorage.getItem('state') ? JSON.parse(sessionStorage.getItem('state')) : {
		adminInfo: {
			avatar: 'default.jpg'
		},
		user: { count: 0 },
		foods: { count: 0 },
		shop_name: null,
		shop__id: null
	}
})
